package compilador.interfaceGrafica;

import compilador.comunicador;
import java.awt.Dimension;
import java.awt.Toolkit;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import javax.swing.filechooser.FileFilter;

/**
 *
 * @author  Joao Luiz
 */
public class Janela extends javax.swing.JFrame {

    private comunicador com;
    private String code;
    
    /**
     * Janela de interface com o usuário
     * para o Analisador Lexico.
     * 
     */
    public Janela() {
        initComponents();

        Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
        //setLocation((d.width-getWidth())/2, (d.height-getHeight())/2);
        
        d.setSize(d.getWidth(), d.getHeight()-30);
        setMinimumSize(d);
        setPreferredSize(d);

        jLayeredPane1.setSize(this.getWidth()-7, this.getHeight()-jMenuBar1.getHeight()-33);
        d = jLayeredPane1.getSize();

        Dimension tamanhoFonte = new Dimension((int)d.getWidth()*3/5, (int)d.getHeight()*2/3);
        frameFonte.setLocation(jLayeredPane1.getX(), jLayeredPane1.getY());
        frameFonte.setSize(tamanhoFonte);

        Dimension tamanhoTokens = new Dimension((int)d.getWidth()*2/5, (int)d.getHeight());
        frameTokens.setLocation(frameFonte.getX()+frameFonte.getWidth(), frameFonte.getY());
        frameTokens.setSize(tamanhoTokens);

        Dimension tamanhoErros = new Dimension((int)d.getWidth()*3/5, (int)d.getHeight()*1/3);
        frameErros.setLocation(frameFonte.getX(), frameFonte.getHeight());
        frameErros.setSize(tamanhoErros);
        
        textAreaCode.requestFocus();

        //jLayeredPane1.setSize((int)d.getWidth()-5, (int)d.getHeight()-35);
    }

    private void analisar() {
        com = new comunicador(code);
        textAreaTokens.setText(com.getTokens());
        textAreaErros.setText(com.getErros());

    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLayeredPane1 = new javax.swing.JLayeredPane();
        frameFonte = new javax.swing.JInternalFrame();
        jScrollPane1 = new javax.swing.JScrollPane();
        textAreaCode = new javax.swing.JTextArea();
        botaoAnalisar = new javax.swing.JButton();
        botaoEdit = new javax.swing.JButton();
        frameTokens = new javax.swing.JInternalFrame();
        jScrollPane2 = new javax.swing.JScrollPane();
        textAreaTokens = new javax.swing.JTextArea();
        botaoLimparTokens = new javax.swing.JButton();
        frameErros = new javax.swing.JInternalFrame();
        jScrollPane3 = new javax.swing.JScrollPane();
        textAreaErros = new javax.swing.JTextArea();
        botaoLimparErros = new javax.swing.JButton();
        jMenuBar1 = new javax.swing.JMenuBar();
        jMenu1 = new javax.swing.JMenu();
        menuItemNovo = new javax.swing.JMenuItem();
        jMenuItem3 = new javax.swing.JMenuItem();
        jMenu2 = new javax.swing.JMenu();
        menuItemLimparTudo = new javax.swing.JMenuItem();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Analisador Léxico - Problema 1 - Linguagens de Programação");
        getContentPane().setLayout(null);

        frameFonte.setIconifiable(true);
        frameFonte.setResizable(true);
        frameFonte.setTitle("Código Fonte");
        frameFonte.setPreferredSize(new java.awt.Dimension(350, 550));
        frameFonte.setVisible(true);
        frameFonte.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseEntered(java.awt.event.MouseEvent evt) {
                frameFonteMouseEntered(evt);
            }
        });

        textAreaCode.setColumns(20);
        textAreaCode.setFont(new java.awt.Font("Monospaced", 1, 14));
        textAreaCode.setRows(5);
        jScrollPane1.setViewportView(textAreaCode);

        botaoAnalisar.setText("Analisar");
        botaoAnalisar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                botaoAnalisarActionPerformed(evt);
            }
        });

        botaoEdit.setText("Editar");
        botaoEdit.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                botaoEditActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout frameFonteLayout = new javax.swing.GroupLayout(frameFonte.getContentPane());
        frameFonte.getContentPane().setLayout(frameFonteLayout);
        frameFonteLayout.setHorizontalGroup(
            frameFonteLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, frameFonteLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(frameFonteLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 320, Short.MAX_VALUE)
                    .addGroup(frameFonteLayout.createSequentialGroup()
                        .addComponent(botaoEdit)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 188, Short.MAX_VALUE)
                        .addComponent(botaoAnalisar)))
                .addContainerGap())
        );
        frameFonteLayout.setVerticalGroup(
            frameFonteLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, frameFonteLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 464, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(frameFonteLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(botaoAnalisar)
                    .addComponent(botaoEdit))
                .addContainerGap())
        );

        frameFonte.setBounds(30, 60, -1, -1);
        jLayeredPane1.add(frameFonte, javax.swing.JLayeredPane.DEFAULT_LAYER);

        frameTokens.setIconifiable(true);
        frameTokens.setResizable(true);
        frameTokens.setTitle("Tokens");
        frameTokens.setPreferredSize(new java.awt.Dimension(350, 550));
        frameTokens.setVisible(true);
        frameTokens.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseEntered(java.awt.event.MouseEvent evt) {
                frameTokensMouseEntered(evt);
            }
        });

        textAreaTokens.setColumns(20);
        textAreaTokens.setEditable(false);
        textAreaTokens.setFont(new java.awt.Font("Monospaced", 1, 14));
        textAreaTokens.setForeground(new java.awt.Color(0, 0, 204));
        textAreaTokens.setRows(5);
        textAreaTokens.setText("Clique em Analisar...");
        jScrollPane2.setViewportView(textAreaTokens);

        botaoLimparTokens.setText("Limpar");
        botaoLimparTokens.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                botaoLimparTokensActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout frameTokensLayout = new javax.swing.GroupLayout(frameTokens.getContentPane());
        frameTokens.getContentPane().setLayout(frameTokensLayout);
        frameTokensLayout.setHorizontalGroup(
            frameTokensLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, frameTokensLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(frameTokensLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jScrollPane2, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 320, Short.MAX_VALUE)
                    .addComponent(botaoLimparTokens))
                .addContainerGap())
        );
        frameTokensLayout.setVerticalGroup(
            frameTokensLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, frameTokensLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 464, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(botaoLimparTokens)
                .addContainerGap())
        );

        frameTokens.setBounds(390, 70, -1, -1);
        jLayeredPane1.add(frameTokens, javax.swing.JLayeredPane.DEFAULT_LAYER);

        frameErros.setIconifiable(true);
        frameErros.setResizable(true);
        frameErros.setTitle("Erros");
        frameErros.setPreferredSize(new java.awt.Dimension(350, 550));
        frameErros.setVisible(true);
        frameErros.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseEntered(java.awt.event.MouseEvent evt) {
                frameErrosMouseEntered(evt);
            }
        });

        textAreaErros.setColumns(20);
        textAreaErros.setEditable(false);
        textAreaErros.setFont(new java.awt.Font("Monospaced", 1, 14));
        textAreaErros.setForeground(new java.awt.Color(255, 51, 0));
        textAreaErros.setRows(5);
        jScrollPane3.setViewportView(textAreaErros);

        botaoLimparErros.setText("Limpar");
        botaoLimparErros.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                botaoLimparErrosActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout frameErrosLayout = new javax.swing.GroupLayout(frameErros.getContentPane());
        frameErros.getContentPane().setLayout(frameErrosLayout);
        frameErrosLayout.setHorizontalGroup(
            frameErrosLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, frameErrosLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(frameErrosLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jScrollPane3, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 320, Short.MAX_VALUE)
                    .addComponent(botaoLimparErros))
                .addContainerGap())
        );
        frameErrosLayout.setVerticalGroup(
            frameErrosLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, frameErrosLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane3, javax.swing.GroupLayout.DEFAULT_SIZE, 464, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(botaoLimparErros)
                .addContainerGap())
        );

        frameErros.setBounds(760, 90, -1, -1);
        jLayeredPane1.add(frameErros, javax.swing.JLayeredPane.DEFAULT_LAYER);

        getContentPane().add(jLayeredPane1);
        jLayeredPane1.setBounds(0, 0, 1250, 700);

        jMenu1.setText("File");

        menuItemNovo.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_B, java.awt.event.InputEvent.CTRL_MASK));
        menuItemNovo.setText("Novo...");
        menuItemNovo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                menuItemNovoActionPerformed(evt);
            }
        });
        jMenu1.add(menuItemNovo);

        jMenuItem3.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_A, java.awt.event.InputEvent.CTRL_MASK));
        jMenuItem3.setText("Abrir fonte...");
        jMenuItem3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem3ActionPerformed(evt);
            }
        });
        jMenu1.add(jMenuItem3);

        jMenuBar1.add(jMenu1);

        jMenu2.setText("Edit");

        menuItemLimparTudo.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_L, java.awt.event.InputEvent.CTRL_MASK));
        menuItemLimparTudo.setText("Limpar Tudo");
        menuItemLimparTudo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                menuItemLimparTudoActionPerformed(evt);
            }
        });
        jMenu2.add(menuItemLimparTudo);

        jMenuBar1.add(jMenu2);

        setJMenuBar(jMenuBar1);

        pack();
    }// </editor-fold>//GEN-END:initComponents

private void botaoAnalisarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_botaoAnalisarActionPerformed

    textAreaErros.setText("");
    textAreaTokens.setText("");

    if(textAreaCode.isEditable())
    {
        code = textAreaCode.getText();

        String[] linhas = code.split("\n", -2);
        String codigoComLinhas = "";
        for(int i=0 ; i<linhas.length ; i++)
        {  
            codigoComLinhas = codigoComLinhas + String.valueOf(i+1) + ": " + linhas[i] + "\n";
        }
        textAreaCode.setText(codigoComLinhas);
        textAreaCode.setEditable(false);
    }    
    analisar();
    
    //textAreaErros.repaint();
    //textAreaTokens.repaint();
    //textAreaCode.repaint();
    
}//GEN-LAST:event_botaoAnalisarActionPerformed

private void jMenuItem3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem3ActionPerformed

    JFileChooser fChooser = new JFileChooser();
    fChooser.setFileFilter(new FileFilter() {

            @Override
            public boolean accept(File f) {
                if(
                    f.getName().endsWith(".dat") || 
                    f.getName().endsWith(".txt") || 
                    f.getName().endsWith(".java") || 
                    f.getName().endsWith(".c") || 
                    f.getName().endsWith(".sql") || 
                    f.getName().endsWith(".cpp") )
                {
                    return true;
                }
                else return false;
            }

            @Override
            public String getDescription() {
                return "Arquivos de texto (*.dat, *.txt, *.java, *.cpp)";
            }
        });
    fChooser.showOpenDialog(this);
    File f = fChooser.getSelectedFile();  
    String str = "";
    
    try
    {
        if(f != null)
        {
            BufferedReader in = new BufferedReader(new FileReader(f));
            while (in.ready())
            {
                str = str + in.readLine()+"\n";
            }
            in.close();
        }
    } catch (IOException e)
    {
        JOptionPane.showMessageDialog(this, "Erro ao ler o arquivo!", "Erro: "+f.getName(), JOptionPane.ERROR_MESSAGE);
    }

    if(!str.equals(""))
    {
        code = str;
        textAreaCode.setText(code);
    }
    
}//GEN-LAST:event_jMenuItem3ActionPerformed

private void botaoLimparTokensActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_botaoLimparTokensActionPerformed
    textAreaTokens.setText("");
}//GEN-LAST:event_botaoLimparTokensActionPerformed

private void botaoLimparErrosActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_botaoLimparErrosActionPerformed
    textAreaErros.setText("");
}//GEN-LAST:event_botaoLimparErrosActionPerformed

private void menuItemLimparTudoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_menuItemLimparTudoActionPerformed
    
    //code = "";

    //textAreaCode.setText("");
    textAreaTokens.setText("");
    textAreaErros.setText("");

    textAreaCode.requestFocus();
    
}//GEN-LAST:event_menuItemLimparTudoActionPerformed

private void menuItemNovoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_menuItemNovoActionPerformed
    
    code = "";

    textAreaCode.setText("");
    textAreaTokens.setText("");
    textAreaErros.setText("");

}//GEN-LAST:event_menuItemNovoActionPerformed

private void frameFonteMouseEntered(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_frameFonteMouseEntered
    frameFonte.toFront();
}//GEN-LAST:event_frameFonteMouseEntered

private void frameTokensMouseEntered(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_frameTokensMouseEntered
    frameTokens.toFront();
}//GEN-LAST:event_frameTokensMouseEntered

private void frameErrosMouseEntered(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_frameErrosMouseEntered
    frameErros.moveToFront();
}//GEN-LAST:event_frameErrosMouseEntered

private void botaoEditActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_botaoEditActionPerformed
    if(!textAreaCode.isEditable())
    {
        textAreaCode.setEditable(true);
        textAreaCode.setText(code);
    }
}//GEN-LAST:event_botaoEditActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton botaoAnalisar;
    private javax.swing.JButton botaoEdit;
    private javax.swing.JButton botaoLimparErros;
    private javax.swing.JButton botaoLimparTokens;
    private javax.swing.JInternalFrame frameErros;
    private javax.swing.JInternalFrame frameFonte;
    private javax.swing.JInternalFrame frameTokens;
    private javax.swing.JLayeredPane jLayeredPane1;
    private javax.swing.JMenu jMenu1;
    private javax.swing.JMenu jMenu2;
    private javax.swing.JMenuBar jMenuBar1;
    private javax.swing.JMenuItem jMenuItem3;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JScrollPane jScrollPane3;
    private javax.swing.JMenuItem menuItemLimparTudo;
    private javax.swing.JMenuItem menuItemNovo;
    private javax.swing.JTextArea textAreaCode;
    private javax.swing.JTextArea textAreaErros;
    private javax.swing.JTextArea textAreaTokens;
    // End of variables declaration//GEN-END:variables

}
